package homebuh.view
{	
	import flash.events.Event;
	
	import homebuh.ApplicationFacade;
	import homebuh.model.LoginProxy;
	import homebuh.model.vo.LoginVO;
	import homebuh.view.components.LoginPanel;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class LoginPanelMediator extends Mediator implements IMediator
	{
		private var loginProxy: LoginProxy;

		public static const NAME:String = 'LoginPanelMediator';

		public function LoginPanelMediator(viewComponent: LoginPanel) 
		{
			super(NAME, viewComponent);
			
			loginProxy = facade.retrieveProxy(LoginProxy.NAME) as LoginProxy;
			
			loginPanel.addEventListener(LoginPanel.TRY_LOGIN, login);
		}

		override public function listNotificationInterests():Array 
		{
			return [ 
						LoginProxy.LOGIN_FAILED,
						LoginProxy.LOGIN_SUCCESS
				   ];
		}

		override public function handleNotification(note: INotification):void 
		{
			switch (note.getName()) 
			{
				case LoginProxy.LOGIN_FAILED:
					loginPanel.statusMessage.htmlText = loginProxy.faultMessage;
					break;
				case LoginProxy.LOGIN_SUCCESS:
					loginPanel.statusMessage.htmlText = "";
					break;
			}
		}
	    
		private function login(event: Event=null ): void
		{			
			var loginVO: LoginVO = new LoginVO();
			loginVO.login = loginPanel.tiLogin.text;
			loginVO.password = loginPanel.tiPassword.text;

			sendNotification(ApplicationFacade.LOGIN, loginVO);
		}

		protected function get loginPanel(): LoginPanel
		{
			return viewComponent as LoginPanel;
		}
	}
}